package cn.ztq.test0614;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.airbnb.lottie.LottieAnimationView;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.config.SelectModeConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.language.LanguageConfig;
import com.ypx.yimagepicker.ImagePicker;
import com.ypx.yimagepicker.bean.ImageItem;
import com.ypx.yimagepicker.bean.MimeType;
import com.ypx.yimagepicker.bean.SelectMode;
import com.ypx.yimagepicker.data.OnImagePickCompleteListener;

import java.util.ArrayList;
import java.util.List;

import cn.ztq.test0614.color.ColorPickerDialog;
import cn.ztq.test0614.color.OnColorPickerListener;
import cn.ztq.test0614.image.ImageAct;
import cn.ztq.test0614.image.SAct;
import cn.ztq.test0614.image.style.WeChatPresenter;


public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener  {


//    private ActivityMainBinding binding;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luncher_main);
        initView();
        setListener();
//        binding = ActivityMainBinding.inflate(getLayoutInflater());
//        setContentView(binding.getRoot());
//
//        BottomNavigationView navView = findViewById(R.id.nav_view);
//        // Passing each menu ID as a set of Ids because each
//        // menu should be considered as top level destinations.
//        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
//                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
//                .build();
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
//        NavigationUI.setupWithNavController(binding.navView, navController);
//
////         startService(new Intent(this, BluetoothService.class));
//        TextToSpeech ff=new TextToSpeech(this,this);
    }

    private void initView() {
//        HomeFragment homeFragment=new HomeFragment();
//        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//        fragmentTransaction.replace(R.id.fl, homeFragment);
//        fragmentTransaction.commit();
        LottieAnimationView animationView = findViewById(R.id.lottieAnimationView);

// 设置动画文件
        animationView.setAnimation("Lottie Logo 2.json");

// 开始播放
        animationView.playAnimation();
    }

    void setListener() {
        findViewById(R.id.tv_color5).setOnClickListener(v -> startActivity(new Intent(this,EQActivity.class)));

        findViewById(R.id.tv_color).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPickerDialog mColorPickerDialog = new ColorPickerDialog(MainActivity.this, getResources().getColor(R.color.colorAccent), true, new OnColorPickerListener() {
                    @Override
                    public void onColorCancel(ColorPickerDialog dialog) {

                    }

                    @Override
                    public void onColorChange(ColorPickerDialog dialog, int color) {

                    }

                    @Override
                    public void onColorConfirm(ColorPickerDialog dialog, int color) {

                    }
                }).show();
            }
        });
//        findViewById(R.id.tv_color2).setOnClickListener(v -> startActivity(new Intent(MainActivity.this, ImageAct.class)));
        findViewById(R.id.tv_color2).setOnClickListener(v -> getAPP());
        findViewById(R.id.tv_color3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(MainActivity.this, BLEActivity.class));
                startActivity(new Intent(MainActivity.this, SAct.class));
            }
        });

        findViewById(R.id.tv_color4).setOnClickListener(v -> {
//            --------------------------------------------
//            mediaLauncher.launch(new PickVisualMediaRequest.Builder()
//                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
//                    .build());
//            ----------------------------------------------------
//            ImagePicker.withMulti(new WeChatPresenter())//指定presenter
//                    //设置选择的最大数
//                    .setMaxCount(1)
//                    //设置列数
//                    .setColumnCount(4)
//                    //设置要加载的文件类型，可指定单一类型
//                    .mimeTypes(MimeType.GIF)
//                    .setPreview(true)//开启预览
//                    .showCamera(false)
//                    //当单选或者视频单选时，点击item直接回调，无需点击完成按钮
//                    .setSinglePickWithAutoComplete(true)
//                    //显示原图时默认原图选项开关
//                    .setDefaultOriginal(false)
//                    //设置单选模式，当maxCount==1时，可执行单选（下次选中会取消上一次选中）
//                    .setSelectMode(SelectMode.MODE_SINGLE)
//                    .setCropRatio(8, 7)
//                    .crop(this, (OnImagePickCompleteListener) items -> {
//                        //图片选择回调，主线程
//                        if (items != null && items.size() > 0) {
//                            for (ImageItem imageItem : items) {
//
//                            }
//                        }
//
//                    });
        });
    }
    ActivityResultLauncher<PickVisualMediaRequest> mediaLauncher=registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri o) {
            Log.d("uri","uri==="+o);
        }
    });
    @Override
    public void onInit(int status) {
        Log.e("语音引擎初始化","success="+TextToSpeech.SUCCESS);
        Log.e("语音引擎初始化","status="+status);
    }

    private void getAPP(){
        PackageManager packageManager = getPackageManager();

        // 获取所有已安装的应用信息
        List<ApplicationInfo> appList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

        for (ApplicationInfo appInfo : appList) {
            // 排除系统应用
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                // 获取应用名称
                String appName = packageManager.getApplicationLabel(appInfo).toString();

                // 获取应用包名
                String packageName = appInfo.packageName;

                // 获取应用图标
                Drawable appIcon = packageManager.getApplicationIcon(appInfo);

                // 打印或使用这些信息
                System.out.println("App Name: " + appName);
                System.out.println("Package Name: " + packageName);
                // 如果你需要在 UI 中显示图标，可以使用 appIcon，例如：
                // imageView.setImageDrawable(appIcon);
            }
        }
    }


}